package org.tc.mq;

import org.apache.activemq.command.ActiveMQTextMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.tc.service.JedisService;
import org.tc.service.impl.ItemServiceImpl;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

/**
 * Created by zxd on 2018/2/1.
 */
public class CustomerMessageListener implements MessageListener{


    @Autowired
    private JedisService jedisService;

    private Logger LOGGER= LoggerFactory.getLogger(CustomerMessageListener.class);

    @Override
    public void onMessage(Message message) {

        //1.获取监听到的商品id
        ActiveMQTextMessage am= (ActiveMQTextMessage) message;

        try {
           String itemId =am.getText();
           LOGGER.info("监听到的商品id为{}",itemId);
            //2.删除缓存
            jedisService.del(ItemServiceImpl.ITEM_CACHE + itemId);
            LOGGER.info("移除商品{}的缓存",itemId);
        } catch (JMSException e) {
            e.printStackTrace();
        }


    }
}
